package com.joymart.user.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 用户缓存，只缓存 B/C 端用户
 * 并且保留其 BusinessRole等信息
 */
@Data
public class CachedUser {

    private User user;
    private BusinessRole highestRole;
    private List<BusinessRole> businessRoles;
    private List<String> userRestAuthorities;
    //被他邀请的人
    @JsonIgnore
    private List<String> downstreamIds;

    public CachedUser(User user,
                      List<String> userRestAuthorities,
                      List<BusinessRole> businessRoles,
                      List<String> downstreamIds) {
        this.user = user;
        this.businessRoles = businessRoles;
        this.userRestAuthorities = userRestAuthorities;
        this.downstreamIds = downstreamIds;
        this.highestRole =
                user.isPartyA()? null:
                businessRoles.isEmpty()? BusinessRole.AG_CSR: Collections.max(businessRoles);
    }

    public CachedUser(User user, List<String> userRestAuthorities) {
       this(user, userRestAuthorities, Collections.emptyList(), Collections.emptyList());
    }

    public CachedUser(User user, List<BusinessRole> businessRoles, List<String> downstreamIds) {
        this(user, Collections.emptyList(), businessRoles, downstreamIds);
    }

    public String getId(){
        return user.getId();
    }

}
